//
//  CZComposePictureView.swift
//  WeiBO28
//
//  Created by 飞奔的羊 on 16/5/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
private let CZComposePictureViewID = "CZComposePictureViewID"
class CZComposePictureView: UICollectionView,UICollectionViewDataSource, UICollectionViewDelegate {
//点击加号按钮的闭包
	var didSelectAddImageClouse:(()->())?
	//保存添加的图片
	lazy var images: [UIImage] = [UIImage]()
	override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
		let flowLayout = UICollectionViewFlowLayout()
		
		super.init(frame: frame, collectionViewLayout: flowLayout)
		setUpUI()
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}

	private func setUpUI() {
		hidden = true
		registerClass(CZComposePictureViewCell.self, forCellWithReuseIdentifier: CZComposePictureViewID)
	dataSource = self
		delegate = self
//		backgroundColor = RandomColor();
	}
	func addImae(image: UIImage) {
		hidden = false
		images.append(image)
		reloadData()
	}
	override func layoutSubviews() {
		super.layoutSubviews()
		//每一项间距
		let itemMargin: CGFloat = 5
		//计算每一项的宽度
		let itemWidth = (frame.size.width - 2 * itemMargin) / 3
		
		let flowLayout = self.collectionViewLayout as! UICollectionViewFlowLayout
		//设置大小
		flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
		//设置间距
		flowLayout.minimumInteritemSpacing = itemMargin
		flowLayout.minimumLineSpacing = itemMargin
		
	}
	// MARK: ------UICollectionViewDataSource------
	func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
		let count = images.count
		if count == 0 || count == 9 {
			return count
		} else {
		
		return count + 1
		}
		
//		return images.count
	}
	func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
		let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CZComposePictureViewID, forIndexPath: indexPath) as! CZComposePictureViewCell
		//判断,当前的 item 小于数组的个数可以取值
		if indexPath.item < images.count {
			
			cell.image = images[indexPath.item]
		} else {
			//显示加号按钮
		cell.image = nil
		}
		cell.deleButonClos = {
			[weak self] in
			
			self?.images.removeAtIndex(indexPath.item)
			
			if self?.images.count == 0  {
    self?.hidden = true
			}
			
			self?.reloadData()
		
		}
		
		return cell
	}
	// MARK: ------UICollectionDelegate 实现方法------
	func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
		//取消选中
		collectionView.deselectItemAtIndexPath(indexPath, animated: true)
		
		//过如果当前索引等于数组个数代表是加号
		if indexPath.item == images.count {
			didSelectAddImageClouse?()
		}
	}
	
}



class CZComposePictureViewCell: UICollectionViewCell {
	//点击删除按钮删除图片
	var deleButonClos: (()->())?
	var image: UIImage? {
		didSet{
			//设置图片
			if image == nil {
				//显示加号图片
			imageView.image = UIImage(named: "compose_pic_add")
			//添加高亮图片
				imageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
				deleteImageButton.hidden = true
			} else {
			
				imageView.image = image
				imageView.highlightedImage = nil
				deleteImageButton.hidden = false
			}
			//
		}
	}
	private lazy var imageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
	private lazy var deleteImageButton: UIButton = {
	let button = UIButton()
		button.setImage(UIImage(named: "compose_photo_close"), forState: .Normal)
	button.addTarget(self, action: #selector(CZComposePictureViewCell.deleClick), forControlEvents: .TouchUpInside)
		
		return button
	}()
	override init(frame: CGRect) {
		super.init(frame: frame)
		setUPUI()
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
	
	
	private func setUPUI(){
//		contentView.backgroundColor = UIColor.redColor()
	contentView.addSubview(imageView)
		contentView.addSubview(deleteImageButton)
		imageView.snp_makeConstraints { (make) in
			make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
		}
		deleteImageButton.snp_makeConstraints { (make) in
			make.top.equalTo(imageView)
			make.trailing.equalTo(imageView)
		}
	
	}
//	点击事件
	@objc private func deleClick(){
	deleButonClos?()
	}
}














